package com.pls.entity;

import java.util.ArrayList;

import java.util.List;

import com.pls.data.DataHelper;
import com.pls.data.EmployeeSeatData;
import com.pls.mapper.EmployeeSeatMapper;

public class EmployeeSeatModifier {

	private EmployeeSeatData empSeatData = new EmployeeSeatData();

	public boolean validateSeatCodeExist(String newSeatCode) {

		boolean isSeatCodeExist = false;

		// EmployeeSeatData empSeatData = new EmployeeSeatData();

		isSeatCodeExist = empSeatData.validateSeatCodeExist(newSeatCode);

		if (isSeatCodeExist == false) {
			System.out.println("Seat Code is not in the seat plan.");
		}
		return isSeatCodeExist;

	}

	/*
	 * public boolean validateEmployeeSeatOccupied(String newSeatCode, boolean
	 * isSeatCodeExist){
	 * 
	 * boolean isEmployeeSeatOccupied = false;
	 * 
	 * // EmployeeSeatData empSeatData = new EmployeeSeatData();
	 * 
	 * isEmployeeSeatOccupied =
	 * empSeatData.validateEmployeeSeatOccupied(newSeatCode, isSeatCodeExist);
	 * 
	 * if (isEmployeeSeatOccupied == false){
	 * System.out.println("Employee Seat is not yet occupied."); } return
	 * isEmployeeSeatOccupied;
	 * 
	 * }
	 */

	public List<EmployeeSeat> selectEmployeeSeatInformation(String newSeatCode,
			boolean isSeatCodeExist) {

		List<EmployeeSeat> result = null;

		if (isSeatCodeExist == true) {

			result = empSeatData.selectEmployeeSeatInformation(newSeatCode,
					isSeatCodeExist);

		}

		return result;

	}

	public int insertEmployeeSeat(int employeeId, String floorLevel, EmployeeSeat empSeat, boolean isSeatCodeExist) {

		int rowCount = 0;

//		EmployeeSeatData empSeatData = new EmployeeSeatData();
		
		rowCount = empSeatData.insertEmployeeSeat(employeeId, floorLevel, empSeat, isSeatCodeExist);
		
		if (rowCount > 0){
			System.out.println("Employee in employee seat is inserted.");
		}
		if (rowCount == 0){
			System.out.println("Employee in employee seat is not inserted.");
		}
		return rowCount;

	}

	public int updateEmployeeSeat(int employeeId, String seatCode, int seatId) {

		int rowCount = 0;

		rowCount = empSeatData.updateEmployeeSeat(employeeId, seatCode, seatId);

		if (rowCount > 0) {
			System.out.println("Employee Seat is updated.");
		}
		if (rowCount == 0){
			System.out.println("Employee Seat is not updated.");
		}
		
		return rowCount;

	}

	public int updateEmployeeSeat(int employeeId, String newSeatCode) {

		int rowCount = 0;
		EmployeeSeat empSeat = null;
		
		EmployeeSeatData empSeatData = new EmployeeSeatData();
		List<EmployeeSeat> empSeats = new ArrayList<EmployeeSeat>();

		// get employee seat details
		empSeat = empSeatData.getEmployeeSeatDetails(employeeId);

		if(empSeat != null)
		{
			// get list of employee seats with same seat code
			empSeats = empSeatData.getEmployeeSeatBySeatCode(empSeat.getSeatCode());

			//choose whether how to delete
			if (empSeats.size() > 1) {
				empSeatData.hardDeleteById(empSeat.getSeatID());
			} else if (empSeats.size() == 1) {
				empSeatData.softDeleteById(empSeat.getSeatID());
			}
		}
		
		else
		{
			empSeat =  new EmployeeSeat();
		}
		empSeat.checkSeatExistence(newSeatCode);
		
		if(!empSeat.isExist())
		{
			System.out.println("Seat Code Destination Does not Exist");
			return 0;
		}
		//check to update employee
		empSeats = empSeatData.getEmployeeSeatBySeatCode(newSeatCode);
		empSeat.setSeatCode(newSeatCode);
		
		//choose whether how to delete
		if (empSeats.size() < 2) {
			rowCount = empSeatData.insertEmployeeSeat(empSeat);
		} 
		else
		{
			System.out.println("Unable to Insert more than two 2 employee in a single seat code! ");
		}
		
		return rowCount;
	}

	public int deleteEmployeeSeat(int employeeId, String seatCode) {

		int rowCount = 0;

		// EmployeeSeatData empSeatData = new EmployeeSeatData();

		rowCount = empSeatData.deleteEmployeeSeat(employeeId, seatCode);

		if (rowCount > 0) {
			System.out.println("Employee Seat is deleted.");
		}
		return rowCount;

	}

}
